package org.jeemp.utils;

import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.io.Encoders;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StrUtil {

    /**
     * 判断字符串是否是纯数字
     *
     * @param str
     *            字符串
     * @return true 纯数字 false 非纯数字
     */
    public static boolean isNumber(String str) {
        if (isEmpty(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) str);
        return matcher.matches();
    }

    public static boolean isEmpty(String s) {
        if (s == null || "".equals(s) || "null".equals(s) || "undefined".equals(s)) {
            return true;
        }
        return s.trim().isEmpty();
    }

    public static boolean isEmpty(String[] ss) {
        if (ss == null) {
            return true;
        }
        return ss.length == 0;
    }

    public static boolean isEmpty(Map<?, ?> map) {
        if (map == null) {
            return true;
        }
        return map.isEmpty();
    }

    public static boolean isEmpty(Object s) {
        if (s == null || "".equals(s) || "null".equals(s) || "undefined".equals(s)) {
            return true;
        }
        return s.toString().isEmpty();
    }

    public static boolean isEmpty(List<?> list) {
        if (list == null) {
            return true;
        }
        return list.isEmpty();
    }

    /**
     * 生成jwt的私钥
     * */
    public static String generateKey() {
        /**
         * SECRET 是签名密钥，只生成一次即可，生成方法：
         * Key key = Keys.secretKeyFor(SignatureAlgorithm.HS256);
         * String secretString = Encoders.BASE64.encode(key.getEncoded()); # 本文使用 BASE64 编码
         * */
        Key key = Keys.secretKeyFor(SignatureAlgorithm.HS512);
        String secretString = Encoders.BASE64.encode(key.getEncoded());
        return secretString;
    }

}
